package cn.wolfcode.wolf2w.business.service.impl;

import cn.wolfcode.wolf2w.business.api.domain.Strategy;
import cn.wolfcode.wolf2w.business.api.domain.StrategyRank;
import cn.wolfcode.wolf2w.business.api.domain.StrategyTheme;
import cn.wolfcode.wolf2w.business.mapper.StrategyMapper;
import cn.wolfcode.wolf2w.business.mapper.StrategyRankMapper;
import cn.wolfcode.wolf2w.business.query.StrategyRankQuery;
import cn.wolfcode.wolf2w.business.service.IStrategyRankService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 攻略排行Service业务层处理
 * 
 * @author fang
 * @date 2024-09-27
 */
@Service
@Transactional
public class StrategyRankServiceImpl extends ServiceImpl<StrategyRankMapper, StrategyRank> implements IStrategyRankService {

    @Autowired
    private StrategyMapper strategyMapper;

    @Override
    public IPage<StrategyRank> queryPage(StrategyRankQuery qo) {
        IPage<StrategyRank> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
        return lambdaQuery()
                .page(page);
    }

    @Override
    public List<StrategyRank> queryByType(int type) {
        //查询时间最新的，查询type，还有前十
        //我可以理解为：现在我要按照不同的类别，根据最近的时间查询出排名前十的信息并且返回
        return lambdaQuery().eq(StrategyRank::getType,type)
                .inSql(StrategyRank::getStatisTime , "SELECT MAX(statis_time) from ta_strategy_rank")
                .last("limit 10")
                .list();
    }

    @Override
    public void statisRank() {
        //查询后放入数据库中
        Date date = new Date();
        LambdaQueryWrapper<Strategy> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Strategy::getIsabroad,1)
                .orderByDesc(Strategy::getViewnum)
                .last("limit 10");
        List<Strategy> abroadList = strategyMapper.selectList(queryWrapper);
        addRankData(abroadList,1L,date);

        queryWrapper.clear();
        queryWrapper.eq(Strategy::getIsabroad,0)
                .orderByDesc(Strategy::getViewnum)
                .last("limit 10");
        List<Strategy> chinaList = strategyMapper.selectList(queryWrapper);
        addRankData(chinaList,2L,date);

        queryWrapper.clear();
        queryWrapper.orderByDesc(Strategy::getViewnum)
                .last("limit 10");
        List<Strategy> hostList = strategyMapper.selectList(queryWrapper);
        addRankData(hostList,3L,date);
    }

/*    @Override
    public List<StrategyRank> queryThemeList() {
        LambdaQueryWrapper<StrategyRank> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(StrategyRank::getStrategyId)
                .inSql(StrategyRank::getStrategyId, "SELECT DISTINCT strategy_id FROM ta_strategy_rank");
        List<Object> strategyIds = baseMapper.selectObjs(wrapper);
        return strategyIds;
    }*/


    private void addRankData(List<Strategy>list,Long type,Date now){
        for (Strategy strategy : list) {
            StrategyRank strategyRank = new StrategyRank();
            strategyRank.setDestId(strategy.getDestId());
            strategyRank.setDestName(strategy.getDestName());
            strategyRank.setType(type);
            strategyRank.setStrategyTitle(strategy.getTitle());
            strategyRank.setStrategyId(strategy.getId());
            strategyRank.setStatisnum(strategy.getViewnum());
            strategyRank.setStatisTime(now);
            baseMapper.insert(strategyRank);
        }
    }
}